package cn.edu.scau.cmi.ChenKaihui.client;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.ChenKaihui.singleTon.Chairman;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;


public class SingleTonBoundary extends Application{
	
	public static Pane pane = new Pane();
	public static ListView<Chairman> chairmanListView = new ListView<>();
    public static TextArea chairmanTextArea = new TextArea();
    
    public static List<Chairman> chairmanList = new ArrayList<Chairman>(); 

	@Override
	public void start(Stage primaryStage) throws Exception {
		initUI(pane, chairmanListView, chairmanTextArea, chairmanList);
		primaryStage.setTitle("模式工厂");
        primaryStage.setScene(new Scene(pane, 1000, 800));
        primaryStage.show(); 
	}
	
	public static void main(String[] args) {
        launch(args);
    }
	
	// 更新列表视图(单例模式)
    private static void updateListView() {
        ObservableList<Chairman> observableList = FXCollections.observableArrayList(chairmanList);
        chairmanListView.setItems(observableList);
    }
	
	//初始化界面
	public static void initUI(Pane pane, ListView<Chairman> chairmanListView, TextArea chairmanTextArea, List<Chairman> chairmanList) {
		
		//创建单例模式按钮
        Button singleTonButton = new Button("创建单例");
        singleTonButton.layoutXProperty().bind(pane.widthProperty().divide(2).subtract(280));
        singleTonButton.setLayoutY(50);
        singleTonButton.setPrefSize(100, 50);
        pane.getChildren().add(singleTonButton);
        
      //创建删除按钮
        Button deleteButton = new Button("删除单例");
        deleteButton.layoutXProperty().bind(pane.widthProperty().divide(2).subtract(120));
        deleteButton.setLayoutY(50);
        deleteButton.setPrefSize(100, 50);
        pane.getChildren().add(deleteButton);
        
        //点击创建按钮
        singleTonButton.setOnAction(e -> {
        	// 创建实例(单例)
            Chairman chairman = Chairman.getInstance();
            chairmanList.add(chairman);
            chairmanTextArea.appendText("--创建对象：毛泽东 \n");
            updateListView();
        });
		
        //点击删除按钮
        deleteButton.setOnAction(e -> {
        	Chairman chairman = Chairman.getInstance();
            if (chairmanList.remove(chairman)) {
                chairmanTextArea.appendText("-删除对象： 毛泽东- \n");
                updateListView();
            }
        });
              
        //输出界面
        chairmanTextArea.layoutXProperty().bind(pane.widthProperty().divide(2).subtract(300));
        chairmanTextArea.layoutYProperty().bind(pane.heightProperty().divide(4).subtract(60));
        chairmanTextArea.setPrefWidth(500);
        chairmanTextArea.setPrefHeight(250);
        pane.getChildren().add(chairmanTextArea);
        
	}
	
}
